/*
 * 补发单新建
 * */
var addOrderId = "";
var resendId = "";
var btnName = "";
var shopId = "";
var projectId = "";
var storId = "";
var operationShow = true;
var addASRGoods = {};
addASRGoods.resendSkuList = null;
addASRGoods.orderGoodsList = null;
addASRGoods.unOrderGoodsList = null;
var isGiftGoodsList = null;
addASRGoods.hidTableDataList = []; // dataTable数据隐藏域，用于父页面获取table数据。
$aftResendSkuTable = null;
var cashReg = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;// 金额正则表达式

$(document).ready(function () {
    var btnName = "";
    addOrderId = extractOrderId();
    resendId = extractResendId();
    btnName = extractBtnText();

    $("#newForm").validate({
        debug: true,
        groups: {
            number: "consTel consMobile"
        },
        rules: {
            consTel: {
                required: {
                    depends: function () {
                        return ($('input[name=consMobile]').val().length <= 0);
                    }
                }
            },
            consMobile: {
                required: {
                    depends: function () {
                        return ($('input[name=consTel]').val().length <= 0);
                    }
                }
            }
        },
        messages: { //提示报错
            consTel: "固定电话和手机号码至少要输入一个。",
            consMobile: "固定电话和手机号码至少要输入一个。"
        },
        errorPlacement: function (error, element) {
            error.insertAfter(element);
        }
    });
    // 手机号码验证// 固话
    $.validator.addMethod("consMobileRule", function (value, element) {
        var rule = /^1[34578]\d{9}$/;
        return this.optional(element) || (rule.test(value));
    }, "您输入的手机号码有误！请重新输入正确手机号！");
    $.validator.addMethod("consTelRule", function (value, element) {
        var rule = /^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
        return this.optional(element) || (rule.test(value));
    }, "您输入的固定电话号码有误！请重新输入正确固定电话！");

    // 初始化dataTable
    initTable();

    // 初始化省列表
    getProvList();
    // 根据订单ID获取订单详细
    getInfoByOrderId(btnName);

    // 初始化物流公司
    setLogisticsCompany();
    // 仓库列表请求接口方法
    getStorage(shopId);
    // 补发原因类型请求接口方法
    getDictList();
    // 获取订单商品列表
    getOrderByID(addOrderId);
    // 获取系统商品列表
    getPrjSkuList();

    if (btnName != null && btnName != "" && btnName != undefined) {
        // 设置弹出层状态
        setTimeout(function () {
            setModelType(btnName);
        }, 50);
    }else{
    	$aftResendSkuTable.column(14).visible(false,true).draw(true);
        $aftResendSkuTable.columns.adjust().draw();
    }
    var lengthTest = $("input[readonly]").length;
    if (lengthTest != 0) {
        $("#tblAddGoods tbody .delete-btn").hide();
        return;
    }
    $("#tblAddGoods").tooltip({
        selector: ".tooltip-column-detail",
        container: "body",
        title: function () {
            if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                return $(this).text();
            }
        }
    });
});

//设置输入框和下拉框不可用的title属性
function setInputSelectTitle(){
		var length = $("input").length;
		for(var i=0;i<length;i++){
			if($("input")[i].disabled || $("input")[i].readOnly){
				$("input")[i].title = $("input")[i].value;
			}
		}
		var sLength = $("select").length;
		for(var i=0;i<sLength;i++){
			if($("select")[i].disabled){
				var childLength = $("select")[i].children.length;
				for(var j=0;j<childLength;j++){
					if($("select")[i].children[j].value == $("select")[i].value){
						$("select")[i].title = $("select")[i].children[j].text;
						break;
					}
				}
				
			}
		}
}

// 按钮事件绑定
$("#orderGoodsAdd").bind("click", function () {
    // 获取表格数据
//    var skuCode = $("#orderGoodsSelect").val();
	var skuCode = $("#rderGoodsDiv div button span").text();
	
	 var attr = skuCode.split("|");
	 skuCode = attr[0];
//	 var skuCodeTemp = attr[0];
	 var skuMark = attr[1];
//    var skuMark = $("#hidOrderGoodsSelect").val();
    if (!skuCode) {
        window.top.window.showScoMessage('error', "请选择商品");
    } else {

        if (skuMark == "" || skuMark == undefined || skuMark==null) {
            skuCode = $("#rderGoodsDiv div button span").text();
        } else {
            skuCode = skuCode + "|" + skuMark;
        }
        // 获取已有数据，并校验是否重复
        var existsCode = $aftResendSkuTable.column(2).data();
        var tblIsGift = $aftResendSkuTable.column(7).data();
//		var tblIsOrder = $aftResendSkuTable.column(6).data();
        var isGift = addASRGoods.orderGoodsList[skuCode].isGift;
        var orderSkuId = addASRGoods.orderGoodsList[skuCode].orderSkuId;
        var orderAttr = "销";
//		var isOrder =  "y";
        var lock = true;// 锁
        if (existsCode.length > 0 && existsCode.search(skuCode).length > 0) {
            for (var i = 0; i < $aftResendSkuTable.data().length; i++) {
                existsCode = $aftResendSkuTable.data()[i].skuCode;
                tblIsGift = $aftResendSkuTable.data()[i].isGift
                var tblOrderSkuId = $aftResendSkuTable.data()[i].orderSkuId;
                if (tblIsGift != "n") {
                    orderAttr = "赠";
                } else {
                    orderAttr = "销";
                }
                
                if (skuMark == "" || skuMark == undefined || skuMark==null) {
                    var skuCodeTep = existsCode + ":" + orderAttr;
                } else {
                    var skuCodeTep = existsCode + ":" + orderAttr + "|" + skuMark;
                }
                if (skuCodeTep == skuCode && orderSkuId == tblOrderSkuId) {
                    window.top.window.showScoMessage('error', "已存在该规格商品");
                    lock = false;
                    break;
                } else {
                    lock = true;
                }
            }
            if (lock) {
                addASRGoods.orderGoodsList[skuCode].isOrderSku = "y";
                addASRGoods.orderGoodsList[skuCode].isGift = isGift;
                var data = addASRGoods.orderGoodsList[skuCode];
                $aftResendSkuTable.row.add(data).draw();
            }

            // 用于父页面获取表数据
            addASRGoods.hidTableDataList = [];
            if ($aftResendSkuTable.data() != undefined) {
                addASRGoods.hidTableDataList = $aftResendSkuTable.data();
                for (var i = 0; i < addASRGoods.hidTableDataList.length; i++) {
                    var qty = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(7).children().val();
                    addASRGoods.hidTableDataList[i].resendQty = qty;
                }
            }
        } else {
            addASRGoods.orderGoodsList[skuCode].isOrderSku = "y";
            addASRGoods.orderGoodsList[skuCode].isGift = isGift;
            var data = addASRGoods.orderGoodsList[skuCode];
            $aftResendSkuTable.row.add(data).draw();
            // 用于父页面获取表数据
            addASRGoods.hidTableDataList = [];
            if ($aftResendSkuTable.data() != undefined) {
                addASRGoods.hidTableDataList = $aftResendSkuTable.data();
                for (var i = 0; i < addASRGoods.hidTableDataList.length; i++) {
                    var qty = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(7).children().val();
                    addASRGoods.hidTableDataList[i].resendQty = qty;
                }
            }
        }
    }
});

// 按钮事件绑定
$("#unOrderGoodsAdd").bind("click", function () {
    // 获取表格数据
//    var skuCode = $("#unOrderGoodsSelect").val();
	var skuCode = $("#unOrderGoodsDiv div button span").text();
    if (!skuCode) {
        window.top.window.showScoMessage('error', "请选择商品");
    } else {
        // 获取已有数据，并校验是否重复
        var existsCode = $aftResendSkuTable.column(2).data();
//		var isGift = addASRGoods.unOrderGoodsList[skuCode].isGifts
        var tblIsGift = $aftResendSkuTable.column(7).data();
        var tblIsOrder = $aftResendSkuTable.column(6).data();
        var isGift = addASRGoods.unOrderGoodsList[skuCode].isGift
        var isOrder = "n";
        var lock = true;// 锁
        if (existsCode.length > 0 && existsCode.search(skuCode).length > 0) {
            for (var i = 0; i < existsCode.length; i++) {
                if (existsCode[i] == skuCode && tblIsGift[i] == isGift && tblIsOrder[i] == isOrder) {
                    window.top.window.showScoMessage('error', "已存在该规格商品");
                    lock = false;
                    break;
                } else {
                    lock = true
                }
            }
            if (lock) {
                addASRGoods.unOrderGoodsList[skuCode].isGift = "n"
                addASRGoods.unOrderGoodsList[skuCode].isOrderSku = "n";
                var data = addASRGoods.unOrderGoodsList[skuCode];
                $aftResendSkuTable.row.add(data).draw();
            }

            // 用于父页面获取表数据
            addASRGoods.hidTableDataList = [];
            if ($aftResendSkuTable.data() != undefined) {
                addASRGoods.hidTableDataList = $aftResendSkuTable.data();
                for (var i = 0; i < addASRGoods.hidTableDataList.length; i++) {
                    var qty = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(7).children().val();
                    addASRGoods.hidTableDataList[i].resendQty = qty;
                }
                $("#hidTableData").data("cacheList", addASRGoods.hidTableDataList);
            } else {
                $("#hidTableData").val("");
            }

        } else {
            addASRGoods.unOrderGoodsList[skuCode].isGift = "n"
            addASRGoods.unOrderGoodsList[skuCode].isOrderSku = "n";
            var data = addASRGoods.unOrderGoodsList[skuCode];
            $aftResendSkuTable.row.add(data).draw();
            // 用于父页面获取表数据
            addASRGoods.hidTableDataList = [];
            if ($aftResendSkuTable.data() != undefined) {
                addASRGoods.hidTableDataList = $aftResendSkuTable.data();
                for (var i = 0; i < addASRGoods.hidTableDataList.length; i++) {
                    var qty = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(7).children().val();
                    addASRGoods.hidTableDataList[i].resendQty = qty;
                }
                $("#hidTableData").data("cacheList", addASRGoods.hidTableDataList);
            } else {
                $("#hidTableData").val("");
            }
        }
    }
});

// 按钮事件绑定
$("#giftGoodsAdd").bind("click", function () {
    // 获取表格数据
//    var skuCode = $("#giftGoodsSelect").val();
	var skuCode = $("#giftGoodsDiv div button span").text();
//	var giftList = isGiftGoodsList;
    if (!skuCode) {
        window.top.window.showScoMessage('error', "请选择商品");
    } else {
        // 获取已有数据，并校验是否重复
        var existsCode = $aftResendSkuTable.column(2).data();
        var tblIsGift = $aftResendSkuTable.column(7).data();
        var tblIsOrder = $aftResendSkuTable.column(6).data();
//		var tblIsGifts = $aftResendSkuTable.data()
        var isGift = isGiftGoodsList[skuCode].isGift;
        isGift = "y";
        var isOrder = "n";
        var lock = true;// 锁
        if (existsCode.length > 0 && existsCode.search(skuCode).length > 0) {
            for (var i = 0; i < existsCode.length; i++) {
                if (existsCode[i] == skuCode && tblIsGift[i] == isGift && tblIsOrder[i] == isOrder) {
                    window.top.window.showScoMessage('error', "已存在该规格商品");
                    lock = false;
                    break;
                } else {
                    lock = true
                }
            }
            if (lock) {
                isGiftGoodsList[skuCode].isGift = "y";
                isGiftGoodsList[skuCode].isGifts = "n";
                isGiftGoodsList[skuCode].isOrderSku = "n";
                var data = isGiftGoodsList[skuCode];
                $aftResendSkuTable.row.add(data).draw();
            }

            // 用于父页面获取表数据
            addASRGoods.hidTableDataList = [];
            if ($aftResendSkuTable.data() != undefined) {
                addASRGoods.hidTableDataList = $aftResendSkuTable.data();
                for (var i = 0; i < addASRGoods.hidTableDataList.length; i++) {
                    var qty = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(7).children().val();
                    addASRGoods.hidTableDataList[i].resendQty = qty;
                }
                $("#hidTableData").data("cacheList", addASRGoods.hidTableDataList);
            } else {
                $("#hidTableData").val("");
            }

        } else {
            isGiftGoodsList[skuCode].isGift = "y";
            isGiftGoodsList[skuCode].isGifts = "n";
            isGiftGoodsList[skuCode].isOrderSku = "n";
            var data = isGiftGoodsList[skuCode];
            $aftResendSkuTable.row.add(data).draw();
            // 用于父页面获取表数据
            addASRGoods.hidTableDataList = [];
            if ($aftResendSkuTable.data() != undefined) {
                addASRGoods.hidTableDataList = $aftResendSkuTable.data();
                for (var i = 0; i < addASRGoods.hidTableDataList.length; i++) {
                    var qty = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(7).children().val();
                    addASRGoods.hidTableDataList[i].resendQty = qty;
                }
                $("#hidTableData").data("cacheList", addASRGoods.hidTableDataList);
            } else {
                $("#hidTableData").val("");
            }
        }
        for (var i = 0; i < $("#tblAddGoods").find("tbody").children("tr").length; i++) {

            var tempIsGift = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(7).text();
            var tempisOrderSku = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(6).text();
            if (tempIsGift == "是" && tempisOrderSku == "否") {
//				$("#tblAddGoods").find("tbody").children("tr").eq(i)
                $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").css("background", "yellow");
                $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(10).children().val("0")
                $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(10).children().attr("disabled", "true")
            }
        }


    }

});

/*
 * 初始化table
 */
function initTable() {
    if (null == $aftResendSkuTable) {// 340px
        $aftResendSkuTable = $("#tblAddGoods")
            .DataTable(
                {
                    "processing": true,
                    "dom": '<"bottom"rtflp>',
                    "searching": false,
                    "scrollX": "100%",
                    "scrollCollapse": false,
                    "destroy": true,
                    "paging": false,
                    "deferRender": true,
                    "columnDefs": [{
                        "className": "truncate-text tooltip-column-detail text-left",
                        "targets": [0]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-left",
                        "targets": [1]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-left",
                        "targets": [2]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-left",
                        "targets": [3]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-left",
                        "targets": [4]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-left",
                        "targets": [5]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-left",
                        "targets": [6]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-left",
                        "targets": [7]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-right",
                        "targets": [8]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-right",
                        "targets": [9]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-right",
                        "targets": [10]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-right",
                        "targets": [11]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-right",
                        "targets": [12]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-right",
                        "targets": [13]
                    }, {
                        "className": "truncate-text tooltip-column-detail text-right",
                        "targets": [14,15]
                    }, {
                        "targets": [16],
                        "visible": operationShow
                    }],
                    "columns": [
                        {
                            "data": "goodsCode",
                            "render": function (data, type, row, meta) {
                                if (row.orderSkuId != null && row.orderSkuId != "" && row.orderSkuId != undefined) {
                                    returnValue = data + "<input type='hidden' value='" + row.orderSkuId + "'/>";
                                } else {
                                    returnValue = data
                                }
                                return returnValue;
                            }
                        },
                        {
                            "data": "goodsName",
                            "render": function (data, type, row, meta) {
                            	var returnValue = "";
                                returnValue = data + "<input type='hidden' value='" + row.goodsSkuCost + "'/>";
                                return returnValue;
                            }
                        },
                        {
                            "data": "skuCode"
                        },
                        {
                            "data": "skuName"
                        },
                        {
                            "data": "ecItemName",
                            "defaultContent": ""
                        },
                        {
                            "data": "ecSkuCode",
                            "orderable": false,
                            "defaultContent": ""
                        },
                        {
                            "data": "isOrderSku",
                            "orderable": false,
                            "defaultContent": "",
                            "render": function (data) {
                                var returnValue = "";
                                if (data != undefined && data != "" && data != null) {
                                    if (data == "y") {
                                        returnValue = "是";
                                    } else if (data == "n") {
                                        returnValue = "否";
                                    }
                                } else {
                                    returnValue = "";
                                }
                                return returnValue;
                            }
                        },
                        {
                            "data": "isGift",
                            "orderable": false,
                            "defaultContent": "",
                            "render": function (data, type, row, meta) {
                                var returnValue = "";
                                if (data != undefined && data != "" && data != null) {
                                    if (data == "y") {
                                        returnValue = "是";
//				                                	if(row.isGifts!= null &&row.isGifts!= "" && row.isGifts != undefined){
//				                                		if(row.isGifts == "n"){
//					                                		returnValue = "是";
//					                                	}else{
//					                                		returnValue = "否";
//					                                	}
//				                                	}else{
//				                                		returnValue = "否";
//				                                	}
                                    } else if (data == "n") {
                                        returnValue = "否";
                                    }
                                } else {
                                    returnValue = "";
                                }
                                return returnValue;
                            }
                        },
                        {
                            "data": "orderQty",
                            "orderable": false,
                            "defaultContent": ""
                        },
                        {
                            "data": "resendQty",
                            "defaultContent": "<input type='text' name='resendQty' style='text-align: right;' class='form-control' onKeyUp='this.value=this.value.replace(/\\D/g,"
                            + "\"\"" + ")' value=''/>",
                            "render": function (data) {
                                var returnValue = "";
                                if (data != undefined && data != "" && data != null) {
                                    returnValue = "<input type='text' name='resendQty' style='text-align: right;' class='form-control' onKeyUp='this.value=this.value.replace(/\\D/g,"
                                        + "\"\"" + ")' value='" + data + "'/>";
                                } else {
                                    returnValue = "<input type='text' name='resendQty' style='text-align: right;' class='form-control' onKeyUp='this.value=this.value.replace(/\\D/g,"
                                        + "\"\"" + ")' value=''/>";
                                }
                                return returnValue;
                            }
                        }, {
                            "data": "factUnitPrice",
                            "orderable": false,
                            "defaultContent": "<input type='text' name='factUnitPrice' style='text-align: right;' class='form-control'  value=''/>",
                            "render": function (data) {
                                var returnValue = "";
                                if (data != undefined && data != "" && data != null) {
                                    returnValue = "<input type='text' name='factUnitPrice' style='text-align: right;' class='form-control'  value='" + data + "'/>";
                                } else {
                                    returnValue = "<input type='text' name='factUnitPrice' style='text-align: right;' class='form-control'  value=''/>";
                                }
                                return returnValue;
                            }
                        }

                        , {
                            "data": "actualAmount",
                            "orderable": false,
                            "render": function (data) {
                                var returnValue = "";
                                if (data != undefined && data != "" && data != null) {
                                    returnValue = data;
                                } else {
                                    returnValue = "0.00";
                                }
                                return returnValue;
                            }
                        }, {
                            "data": "divideGoodsFee",
                            "orderable": false,
                            "render": function (data) {
                                var returnValue = "";
                                if (data != undefined && data != "" && data != null) {
                                    returnValue = data;
                                } else {
                                    returnValue = "0.00";
                                }
                                return returnValue;
                            }
                        }, {
                            "data": "divideOrderFeeShow",
                            "render": function (data) {
                                var returnValue = "";
                                if (data != undefined && data != "" && data != null) {
                                    returnValue = data;
                                } else {
                                    returnValue = "0.00";
                                }
                                return returnValue;
                            }
                        },{
                            "data": "goodsSkuCost",
                            "render": function (data) {
                                var returnValue = "";
                                if (data != undefined && data != "" && data != null) {
                                    returnValue = data;
                                } else {
                                    returnValue = "0.00";
                                }
                                return returnValue;
                            }
                        }, {
                            "data": "sn",
                            "visible":false,
                            "defaultContent":""
                        }, {
                            "data": "operation",
                            "defaultContent": "<a class='delete-btn' type='button'><i class='fa fa-trash-o'></i></a>",
                            "render": function (data) {
                                var returnValue = "";
                                if (data != "") {
                                    returnValue = "<a class='delete-btn' type='button'><i class='fa fa-trash-o'></i></a>";
                                } else {
                                    returnValue = "<a class='delete-btn' type='button'><i class='fa fa-trash-o'></i></a>";
                                }
                                return returnValue;
                            }
                        }]
                }).on('draw.dt', function () {
                    // 如果是查看界面，且状态是已完成，就显示sn列
                    var isDetail = $("#actionType").val()=="detail";
                    
                    var isComplete = $("#aftersaleresendstatus").val()=="z";
                    if(isComplete&&isDetail){
                        $aftResendSkuTable.column(15).visible(true);
                    }
                });

        $aftResendSkuTable.on("change", "[name='resendQty']", function () {
            var thisTr = $(this).parents("tr");
            var skuCode = thisTr.children('td').eq(2).text();
            var qty = $(this).val();// 补发数量
            var reg = /^\+?[1-9]\d*$/;
            if (!reg.test(qty)) {
                window.top.window.showModalAlert("请输入大于零的正整数！");
                $(this).val("");
                return
            }
            var factUnitPrice = thisTr.children('td').eq(10).children().val();// 实际单价
            var actualPayPrice = thisTr.children('td').eq(12)[0].textContent;// 实际支付单价
            var actualAmount = qty * factUnitPrice;// 实际金额
            if (actualPayPrice == undefined) {
                actualPayPrice = 0;
            }
            var divideOrderFee = qty * actualPayPrice;// 实际支付金额
            thisTr.children('td').eq(11).text(actualAmount);// 实际金额
            thisTr.children('td').eq(13).text(divideOrderFee);// 实际支付金额
            // 设置提交的值
            for (var i = 0; i < addASRGoods.hidTableDataList.length; i++) {
                var qtyTem = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(9).children().val();
                addASRGoods.hidTableDataList[i].resendQty = qtyTem;
            }
        });
        $aftResendSkuTable.on("change", "[name='factUnitPrice']", function () {
            var thisTr = $(this).parents("tr");
            var skuCode = thisTr.children('td').eq(2).text();
            var isGift = thisTr.children('td').eq(7).text();
            var factUnitPrice = $(this).val();// 实际单价
            if (factUnitPrice == "") {
                window.top.window.showModalAlert("实际单价不能为空！");
                return
            }
            if (isGift == "否") {
                if (!cashReg.test(factUnitPrice)) {
                    window.top.window.showModalAlert("请输入大于等于零的正数，并且只能有两位小数！");
//					 $(this).val("");
                    return
                }
            } else {
                if (factUnitPrice != "0") {
                    if (!cashReg.test(factUnitPrice)) {
                        window.top.window.showModalAlert("请输入大于等于零的正数，并且只能有两位小数！");
//						 $(this).val("");
                        return
                    }
                }
            }


            var qty = thisTr.children('td').eq(9).children().val();// 补发数量
            var actualPayPrice = thisTr.children('td').eq(12)[0].textContent;// 实际支付单价
            var actualAmount = qty * factUnitPrice;// 实际金额
            if (actualPayPrice == undefined) {
                actualPayPrice = 0;
            }
            var divideOrderFee = qty * actualPayPrice;// 实际支付金额
            thisTr.children('td').eq(11).text(actualAmount);// 实际金额
            thisTr.children('td').eq(13).text(divideOrderFee);// 实际支付金额
            // 设置提交的值
            for (var i = 0; i < addASRGoods.hidTableDataList.length; i++) {
                var qtyTem = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(9).children().val();
                addASRGoods.hidTableDataList[i].resendQty = qtyTem;
            }
        });

        $aftResendSkuTable.on("click", ".delete-btn", function () {
            var lengthTest = $("input[readonly]").length;
            if (lengthTest != 0) {
                $(this).hide();
                return;
            }
            var row = $aftResendSkuTable.row($(this).parents("tr"));
            row.remove();
            $aftResendSkuTable.draw();


            // 用于父页面获取表数据
            addASRGoods.hidTableDataList = [];
            if ($aftResendSkuTable.data() != undefined) {
                addASRGoods.hidTableDataList = $aftResendSkuTable.data();
                for (var i = 0; i < addASRGoods.hidTableDataList.length; i++) {
                    var qty = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(9).children().val();
                    addASRGoods.hidTableDataList[i].resendQty = qty;
                }
                $("#hidTableData").data("cacheList", addASRGoods.hidTableDataList);
            } else {
                $("#hidTableData").val("");
            }

        });
    }
}

/*
 * model 获取省信息
 */
function getProvList() {
    // 省 初期化
    $.ajax({
        type: "GET",
        url: "/area/getProvList.action",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.result == "success") {
                $("#provName option").remove();
                $("#provName").append("<option value=''>请选择</option>");
                $.each(data.data, function (n, value) {
                    $('#provName').append("<option value='" + value.areaCode + "'>" + value.areaName + "</option>");
                });
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });
}

/*
 * model 获取市列表
 */
function setCtiyList() {
    $.ajax({
        type: "GET",
        url: "/area/getCityList.action?provId=" + $("#provName").val(),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: false,// 同步
        success: function (data) {
            if (data.result == "success") {
                $("#cityName option").remove();
                $("#cityName").append("<option value=''>请选择</option>");
                $("#cityName option").remove();
                $("#cityName").append("<option value=''>请选择</option>");
                $.each(data.data, function (n, value) {
                    $('#cityName').append("<option  value='" + value.areaCode + "'>" + value.areaName + "</option>");
                });
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });
}

/*
 * model 获取区列表
 */
function setDistList() {
    $.ajax({
        type: "GET",
        url: "/area/getDistList.action?cityId=" + $("#cityName").val(),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: false,// 同步
        success: function (data) {
            if (data.result == "success") {
                $("#distName option").remove();
                $("#distName").append("<option value=''>请选择</option>");
                $.each(data.data, function (n, value) {
                    $('#distName').append("<option value='" + value.areaCode + "'>" + value.areaName + "</option>");
                });
//                $('.selectpicker').selectpicker('refresh');
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });
};

/*
 * 获取根据订单id获取订单详细
 */
function getInfoByOrderId(btnName) {
    

    $.ajax({
        type: "GET",
        url: "/order/getOrderInfo.action",
        contentType: "application/json;charset=utf-8",
        data: {
            "orderId": addOrderId
        },
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.result == "success") {
                
                setOrderInfo(data.data, btnName)
                shopId = data.data.shopId;
                projectId = data.data.prjId;
                storId = data.data.storId;
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                window.top.window.showScoMessage('error', result.errorObject.errorText);
            }
        }
    });
}

function setOrderInfo(data, btnName) {
    $("#sourceNo").val(data.orderCode); // 订单号
    $("#tradeNo").val(data.tradeCode);// 平台交易号
    $("#storId").val(data.shopName);// 网店
    $("#buyerNick").val(data.buyerNick);// 客户ID
    $("#orderId").val(data.orderId);
    if (btnName == null || btnName == "" || btnName == undefined) {
        $("#cons").val(data.cons);
        $("#consMobile").val(data.consMobile);
        $("#consTel").val(data.consTel);
        $("#consAddr").val(data.consAddr);
        $("#provName").find("option:contains('" + data.provName + "')").attr("selected", true);
        setCtiyList();
        $("#cityName").find("option:contains('" + data.cityName + "')").attr("selected", true);
        setDistList();
        $("#distName").find("option:contains('" + data.distName + "')").attr("selected", true);
    }
    //$("#lgstCode").val(data.orderCode); // 物流公司code
}

/*
 * 修改或者查看时获取补发单详细信息
 */
function getInfoByResendId() {
    $.ajax({
        type: "GET",
        url: "/afterSaleResend/getResendById.action",
        contentType: "application/json;charset=utf-8",
        data: {
            "resendId": resendId
        },
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.result == "success") {
                setResendInfo(data.data);
                $("#provName").find("option:contains('" + data.data.provName + "')").attr("selected", true);
                setCtiyList();
                $("#cityName").find("option:contains('" + data.data.cityName + "')").attr("selected", true);
                setDistList();
                $("#distName").find("option:contains('" + data.data.distName + "')").attr("selected", true);
                addASRGoods.resendSkuList = [];
                if (data.data.resultSkuVOList.length > 0) {
                    for (var i = 0; i < data.data.resultSkuVOList.length; i++) {
                        addASRGoods.resendSkuList.push(data.data.resultSkuVOList[i]);
                    }
                }
                if (data.data.resendSkuList.length > 0) {
                    for (var i = 0; i < data.data.resendSkuList.length; i++) {
                        addASRGoods.resendSkuList.push(data.data.resendSkuList[i]);
                    }
                }
                for (var i = 0; i < addASRGoods.resendSkuList.length; i++) {
                    var resendList = addASRGoods.resendSkuList[i];
                    $aftResendSkuTable.row.add(resendList).draw();
                }
                for (var i = 0; i < $("#tblAddGoods").find("tbody").children("tr").length; i++) {
                    var tempIsGift = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(7).text();
                    var tempisOrderSku = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(6).text();
                    if (tempIsGift == "是" && tempisOrderSku == "否") {
                        $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").css("background", "yellow");
                        $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(10).children().val("0")
                        $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(10).children().attr("disabled", "true")
                    }
                }

            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                window.top.window.showScoMessage('error', result.errorObject.errorText);
            }
        }
    });
}

/*
 * 显示带回信息
 */
function setResendInfo(data) {
    window.top.window.currentResendData = data;
    var provName = data.provName
    $("#storName").val(data.storId);
    $("#ResendReasonType").val(data.resendReasonId);
    $("#lgstIdNew").val(data.lgstId);
    $("#cons").val(data.cons);
    $("#consMobile").val(data.consMobile);
    $("#consTel").val(data.consTel);
    $("#lgstNo").val(data.lgstNo);
//	$("#postcodes").val(data.areaCoding);

    $("#consAddr").val(data.consAddr);
    $("#remark").val(data.remark);
    
    $("#aftersaleresendstatus").val(data.resendStatus);
}

/*
 * 获取订单商品列表
 */
function getOrderByID(orderId) {
    $.ajax({
        type: "GET",
        url: "/afterSaleResend/getOrderSkuById.action",
        contentType: "application/json;charset=utf-8",
        data: {
            "orderId": addOrderId
        },
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.result == "success") {
                addOderGoodsList(data.data);

                // sysRefundEdit.initOrderSkuSelectListWithData(data.data);
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                window.top.window.showScoMessage('error', result.errorObject.errorText);
            }
        }
    });
};

/*
 * 设置弹出层是否可更改
 */
function setModelType(btnText) {

    if (btnText == "check") {
        getInfoByResendId();
        $("#examine").hide();// 按钮显示
        $("#cancelBtn").hide();// 按钮显示
        $("#rderGoodsDiv").hide();// 订单商品选择隐藏
        $("#unOrderGoodsDiv").hide();// 非订单商品选择隐藏
        $("#giftGoodsDiv").hide();// 赠品选择隐藏
        $("#actionType").val("detail");// 赠品选择隐藏
        $("#modelBody input").attr("readonly", "true");
        $("#modelBody select").attr("disabled", "true");
        $aftResendSkuTable.column(14).visible(true,true).draw(true);
        $aftResendSkuTable.columns.adjust().draw();
    }
    if (btnText == "edit") {
        getInfoByResendId();
        addASRGoods.hidTableDataList = $aftResendSkuTable.data();
        for (var i = 0; i < addASRGoods.hidTableDataList.length; i++) {
            var qty = $("#tblAddGoods").find("tbody").children("tr").eq(i).children("td").eq(7).children().val();
            addASRGoods.hidTableDataList[i].resendQty = qty;
        }
        $aftResendSkuTable.column(14).visible(false,true).draw(true);
        $aftResendSkuTable.columns.adjust().draw();
    }
    setInputSelectTitle();

}

/*
 * 获取非订单商品列表
 */
function getPrjSkuList() {
    var searchCondition = { // 高级检索
        // "resendId" : "", // 补发单ID
        "prjId": projectId
    };
    $.ajax({
        type: "GET",
        url: "/afterSaleResend/getPrjSkuList.action",
        contentType: "application/json;charset=utf-8",
        // data :{"formJson":JSON.stringify(searchCondition)},
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.result == "success") {
                
                var isGifts = [];
//			    isGiftGoodsList = [];
                for (var index in data.data) {
                    if (data.data[index].isGift == "y") {
                        $("#giftGoodsSelect").append(
                            "<option value='" + data.data[index].skuCode + "' data-subtext='" + data.data[index].goodsName + "|" + data.data[index].skuName + "'>" + data.data[index].skuCode + "</option>");
                        isGifts[data.data[index].skuCode] = data.data[index];
                        //var newObject = jQuery.extend(true, {}, oldObject)
                    }
                }
                isGiftGoodsList = jQuery.extend(true, {}, isGifts);
                addUnOderGoodsList(data.data);
                
                $('#giftGoodsSelect').selectpicker('refresh');
                for(var i=0;i<$("#giftGoodsDiv li").length;i++){
                	$("#giftGoodsDiv li:eq("+i+") span").text();
                	var smallTitle = $("#giftGoodsDiv li:eq("+i+") span small").text();
                	var obj = $("#giftGoodsDiv li:eq("+i+") span").clone();
                	obj.find(':nth-child(n)').remove();
                	$("#giftGoodsDiv li:eq("+i+") span").attr("title",obj.text());
                	$("#giftGoodsDiv li:eq("+i+") span small").attr("title",smallTitle);
                }
                
//			    console.log(isGiftGoodsList);
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                window.top.window.showScoMessage('error', result.errorObject.errorText);
            }
        }
    });
};

/*
 * 将订单商品添加到一个列表中
 */
function addOderGoodsList(data) {
    
    var orderSkuData = {};
    if (data.length > 0) {
        var values = {};
        var lock = false;
        for (var index in data) {
            values[index] = data[index].skuCode + ":" + data[index].skuProperty;
        }
        for (var index in data) {
            for (var index1 in data) {
                if (values[index] == values[index1]) {
                    if (index != index1) {
                        lock = true;
                        //return;
                    }
                }
            }
            data[index].orderQty = data[index].qty;
            if (lock) {
                orderSkuData[data[index].skuCode + ":" + data[index].skuProperty + "|" + index] = data[index];//
                $("#orderGoodsSelect").append(
                    "<option value='" + data[index].skuCode + ":" + data[index].skuProperty + "|" + index + "' data-subtext ='["+data[index].skuSource+"]" + data[index].goodsName + "|" + data[index].skuName + "|实际支付金额:"
                    + data[index].divideOrderFee + "|购买件数:" + data[index].qty + "|" + data[index].skuProperty + "'>" + data[index].skuCode + ":" + data[index].skuProperty + "|" + index + "</option>");
            } else {
                orderSkuData[data[index].skuCode + ":" + data[index].skuProperty] = data[index];//
                $("#orderGoodsSelect").append(
                    "<option value='" + data[index].skuCode + ":" + data[index].skuProperty + "' data-subtext='["+data[index].skuSource+"]" + data[index].goodsName + "|" + data[index].skuName + "|实际支付金额:"
                    + data[index].divideOrderFee + "|购买件数:" + data[index].qty + "|" + data[index].skuProperty + "'>" + data[index].skuCode + ":" + data[index].skuProperty + "</option>");
            }

        }
        
        $('#orderGoodsSelect').selectpicker('refresh');
        for(var i=0;i<$("#rderGoodsDiv li").length;i++){
        	$("#rderGoodsDiv li:eq("+i+") span").text();
        	var smallTitle = $("#rderGoodsDiv li:eq("+i+") span small").text();
        	var obj = $("#rderGoodsDiv li:eq("+i+") span").clone();
        	obj.find(':nth-child(n)').remove();
        	$("#rderGoodsDiv li:eq("+i+") span").attr("title",obj.text());
        	$("#rderGoodsDiv li:eq("+i+") span small").attr("title",smallTitle);
        }
    }
    addASRGoods.orderGoodsList = orderSkuData;
};

function inputSelect(value) {
    var attr = value.split("|");
    value = attr[0];
    var temp = attr[1];
    $("#orderGoodsSelect").val(value);
    $("#hidOrderGoodsSelect").val(temp);
};

/*
 * 非订单商品数据绑定
 */
function addUnOderGoodsList(data) {
    var orderSkuData = {};
//	var isGifts ={};
    if (data.length > 0) {
        for (var index in data) {
            // data[index].orderQty = data[index].qty;

            $("#unOrderGoodsSelect").append(
                "<option value='" + data[index].skuCode + "' data-subtext='" + data[index].goodsName + "|" + data[index].skuName + "'>" + data[index].skuCode + "</option>");
//			if(data[index].isGift == "y"){
//				data[index].isGifts="n";
////				isGifts[data[index].skuCode] = data[index];
//				$("#giftGoodsList").append(
//				        "<option value='" + data[index].skuCode + "' label='" + data[index].goodsName + "|" + data[index].skuName + "|实际支付金额:"
//		                + data[index].divideOrderFee + "|件数:" + data[index].qty + "'></option>");
//			}
            if(data[index].isGift == "y"){
            	data[index].isGift="n";
            }
            orderSkuData[data[index].skuCode] = data[index];
        }
        $('#unOrderGoodsSelect').selectpicker('refresh');
        for(var i=0;i<$("#unOrderGoodsDiv li").length;i++){
        	$("#unOrderGoodsDiv li:eq("+i+") span").text();
        	var smallTitle = $("#unOrderGoodsDiv li:eq("+i+") span small").text();
        	var obj = $("#unOrderGoodsDiv li:eq("+i+") span").clone();
        	obj.find(':nth-child(n)').remove();
        	$("#unOrderGoodsDiv li:eq("+i+") span").attr("title",obj.text());
        	$("#unOrderGoodsDiv li:eq("+i+") span small").attr("title",smallTitle);
        }
    }
    addASRGoods.unOrderGoodsList = orderSkuData;
};

/*
 * 子页面保存方法
 */
function saveResend(context) {
    if (!$("#newForm").valid()) {
//		$('#modalResendDetail', window.top.document).find("#btnResendClose").click();
        return;
    }
    var resendSkuVOList = [];
    var tableList = addASRGoods.hidTableDataList;
    if (tableList.length < 1) {
        window.top.window.showScoMessage("error", "请至少添加一条商品！");
        return;
    }
    var flg = true;
    for (var i = 0; i < tableList.length; i++) {

        var tempTR = $("#tblAddGoods").find("tbody").children("tr")
        for (var j = 0; j < tempTR.length; j++) {
            var temSkuCode = tempTR.eq(j).children("td").eq(2).text();
            var temOrderSkuId = tempTR.eq(j).children("td").eq(0).children("input").val();
            var goodsSkuCost = tempTR.eq(j).children("td").eq(1).children("input").val();
            var qty = tempTR.eq(j).children("td").eq(9).children().val();
            var isGift = tempTR.eq(j).children("td").eq(7).text();
            var isOrder = tempTR.eq(j).children("td").eq(6).text();
            var factUnitPrice = tempTR.eq(j).children("td").eq(10).children().val();
            if (isGift == "否") {
                var temIsGift = "n";
            } else {
                var temIsGift = "y";
            }
            if (isOrder == "否") {
                var temIsOrder = "n";
            } else {
                var temIsOrder = "y";
            }
            if (temSkuCode == tableList[i].skuCode) {
                if (temIsGift == tableList[i].isGift && temIsOrder == tableList[i].isOrderSku) {
                    if (tableList[i].orderSkuId != null && tableList[i].orderSkuId != "" && tableList[i].orderSkuId != undefined && temOrderSkuId != null && temOrderSkuId != "" && temOrderSkuId != undefined) {
                        if (temOrderSkuId == tableList[i].orderSkuId) {
                            tableList[i].resendQty = qty;
                            tableList[i].factUnitPrice = factUnitPrice;
//                            tableList[i].goodsSkuCost = goodsSkuCost;
                        }
                    } else {
                        tableList[i].resendQty = qty;
                        tableList[i].factUnitPrice = factUnitPrice;
//                        tableList[i].goodsSkuCost = goodsSkuCost;
                    }

                }
            }
        }
    }
    for (var i = 0; i < tableList.length; i++) {

        if (tableList[i].resendQty == "") {
            window.top.window.showModalAlert("补发单数量不能为空！");
            flg = false;
            return;
        }
        if (tableList[i].factUnitPrice == "") {
            window.top.window.showModalAlert("实际单价不能为空！");
            flg = false;
            return;
        }

        if (tableList[i].isGift == "n") {
            if (!cashReg.test(tableList[i].factUnitPrice)) {
                window.top.window.showModalAlert("请输入大于等于零的正数，并且只能有两位小数！");
                flg = false;
                return
            }
        } else {
            if (tableList[i].factUnitPrice != "0") {
                if (!cashReg.test(tableList[i].factUnitPrice)) {
                    window.top.window.showModalAlert("请输入大于等于零的正数，并且只能有两位小数！");
                    flg = false;
                    return
                }
            }
        }

        var item = {
            "resendSkuId": "",
            "qty": "",
            "deleted": "",
            "skuId": "",
            "skuCode": "",
            "skuName": "",
            "goodsId": "",
            "goodsCode": "",
            "goodsName": "",
            "isGift": "",
            "isOrderSku": "",
            "divideGoodsFee": "",
            "factUnitPrice": "",
            "ecOid": "",
            "sourceSkuId": ""
        };
        resendSkuVOList.push(item);
        if (tableList[i].resendSkuId) {
            resendSkuVOList[i].resendSkuId = tableList[i].resendSkuId;
        }
        resendSkuVOList[i].qty = tableList[i].resendQty;
        resendSkuVOList[i].deleted = tableList[i].deleted;
        resendSkuVOList[i].skuId = tableList[i].skuId;
        resendSkuVOList[i].skuCode = tableList[i].skuCode;
        resendSkuVOList[i].skuName = tableList[i].skuName;
        resendSkuVOList[i].goodsId = tableList[i].goodsId;
        resendSkuVOList[i].goodsCode = tableList[i].goodsCode;
        resendSkuVOList[i].goodsName = tableList[i].goodsName;
        resendSkuVOList[i].isGift = tableList[i].isGift;
        resendSkuVOList[i].isOrderSku = tableList[i].isOrderSku;
        resendSkuVOList[i].divideGoodsFee = tableList[i].divideGoodsFee;
        resendSkuVOList[i].factUnitPrice = tableList[i].factUnitPrice;
//        resendSkuVOList[i].goodsSkuCost = tableList[i].goodsSkuCost;
        if (tableList[i].isOrderSku == "y") {
            resendSkuVOList[i].ecOid = tableList[i].oid;
            resendSkuVOList[i].sourceSkuId = tableList[i].orderSkuId;
        } else {
            resendSkuVOList[i].ecOid = "";
            resendSkuVOList[i].sourceSkuId = tableList[i].prjSkuId;
        }


    }

    if (!flg) {
        return;
    }
    searchCondition = {
        "sourceType": $("#sourceType").val(),
        "sourceCode": $("#sourceNo").val(),
        "sourceId": $("#orderId").val(),
        "resendId": resendId,
        "tradeNo": $("#tradeNo").val(),
        "shopName": $("#storId").val(),
        "buyerNick": $("#buyerNick").val(),
        "storId": $("#storName").val(),
        "lgstId": $("#lgstIdNew").val(),
        "lgstNo": $("#lgstNo").val(),
        "resendReasonName": $("#ResendReasonType").find("option:selected").text(),
        "resendReasonId": $("#ResendReasonType").val(),
        "cons": $("#cons").val(),
        "consTel": $("#consTel").val(),
        "consMobile": $("#consMobile").val(),
        "consCode": "",
        "provName": $("#provName").find("option:selected").text(),
        "cityName": $("#cityName").find("option:selected").text(),
        "distName": $("#distName").find("option:selected").text(),
        "consAddr": $("#consAddr").val(),
        "remark": $("#remark").val(),
        "resendSkuList": resendSkuVOList
    };
    
    var loading = window.top.window.getIndexBusyOverlay();
    $.ajax({
        type: "POST",
        url: "/afterSaleResend/saveResend.action",
        data: JSON.stringify(searchCondition),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
//        async: false,// 同步
        beforeSend:function(){
            if(loading) {
            	loading.settext("正在处理中，请稍后...");
            }
        },
        success: function (data) {
        	loading.remove();
            if (data.result == "success") {
                window.top.window.showScoMessage("ok", data.data);
                if ($('#refresh', window.top.window.getCurrentIframe().contentWindow.document).length > 0) {
                    $('#refresh', window.top.window.getCurrentIframe().contentWindow.document).click()
                }
                $('#modalResendDetail', window.top.document).find("#btnResendClose").click();
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
        	loading.remove();
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });

}

/**
 * 省变化的时候，市，区的值要变化
 */
$('#provName').on('change', function () {

    $("#cityName").val("");
    $("#distName").val("");
    setCtiyList();

});
/**
 * 市变化的时候，区的值要变化
 */
$('#cityName').on('change', function () {

    $("#distName").val("");
    setDistList();

});

/*
 * 查看点击事件
 */
$('#examine').on('click', function () {
    $(this).parents("#newForm").find("input").attr("readonly", "true");// .
    $(this).parents("#newForm").find("select").attr("readonly", "true");//
    $(this).parents("#newForm").find("#examine").hide();// 按钮隐藏
    $(this).parents("#newForm").find("#cancelBtn").hide();// 按钮隐藏
    $(this).parents("#newForm").find("#addNoOrder").hide();// 按钮隐藏
    $(this).parents("#newForm").find("#addOrder").hide();// 按钮隐藏
    $(this).parents("#newForm").find("#addGift").hide();// 按钮隐藏
})

/**
 * 初始化物流公司
 */
function setLogisticsCompany() {
    // 物流公司 初期化
    $.ajax({
        type: "GET",
        url: "/lgst/getLgstByProjectId.action?projectId=" + projectId,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: false,// 同步
        success: function (data) {
            if (data.result == "success") {
                $("#lgstId option").remove();
                $("#lgstId").append("<option value=''>请选择</option>");
                $.each(data.data, function (n, value) {
                    $('#lgstId').append("<option value='" + value.lgstId + "'>" + value.lgstName + "</option>");
                });
                $("#lgstIdNew option").remove();
                $("#lgstIdNew").append("<option value=''>请选择</option>");
                $.each(data.data, function (n, value) {
                    $('#lgstIdNew').append("<option value='" + value.lgstId + "'>" + value.lgstName + "</option>");
                });
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });
};

/*
 * model仓库列表请求接口方法
 */
function getStorage(id) {
    $.ajax({
        type: "GET",
        url: "/stor/getStorageByShopId.action?shopId=" + id,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: false,// 同步
        success: function (data) {
            if (data.result == "success") {
                $("#storName option").remove();
                $("#storName").append("<option value=''>请选择</option>");
                $.each(data.data, function (n, value) {
                    $('#storName').append("<option value='" + value.storId + "'>" + value.storName + "</option>");
                });
                $("#storName").val(storId);
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });
}

/*
 * model补发原因类型请求接口方法
 */
function getDictList() {
    $.ajax({
        type: "GET",
        url: "/resendReasonType/getResendReasonTypeList.action?length=-1",//"/dictionary/getParamDictByCode.action",
        data: {
            "code": "RESEND_REASON_TYPE"
        },
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: false,// 同步
        success: function (data) {
            if (data.result == "success") {
                $("#ResendReasonType option").remove();
                $("#ResendReasonType").append("<option value=''>请选择</option>");
                $.each(data.data, function (n, value) {
                    $('#ResendReasonType').append("<option value='" + value.resendReasonCode + "'>" + value.resendReasonName + "</option>");
                });
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });
}

// 从url中解析系统退款单ID
function extractId() {
    return extractParam("sysRefundId");
};

// 从url中解析系统订单ID
function extractOrderId() {
    return extractParam("resOrderId");
};

// 从url中解析按钮名称
function extractBtnText() {
    return extractParam("btnType");
};

// resendId
function extractResendId() {
    return extractParam("resendId");
};
// ===========公共方法==========

// 获取页面参数
function GetArgsFromHref(sHref, sArgName) {
    var args = sHref.split("?");
    var retval = "";

    if (args[0] == sHref) /* 参数为空 */
    {
        return retval;
        /* 无需做任何处理 */
    }
    var str = args[1];
    args = str.split("&");
    for (var i = 0; i < args.length; i++) {
        str = args[i];
        var arg = str.split("=");
        if (arg.length <= 1)
            continue;
        if (arg[0] == sArgName)
            retval = arg[1];
    }
    return retval;
}

function extractParam(paramName) {
    var returnString = "";
    var str = window.location.search;
    if (!!str) {
        returnString = GetArgsFromHref(str, paramName);
    }
    return returnString;
}
